package com.ebelter.myehc.ui.fragments.modifys;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.ebelter.btcomlib.bases.fragment.BaseFragment;

import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.myehc.R;
import com.ebelter.myehc.models.http.request.EhcNetUtils;

import com.ebelter.myehc.ui.activitys.MainActivity;
import com.ebelter.myehc.ui.view.CombinView6;
import com.ebelter.myehc.utils.ViewUtilsEHC;

import org.greenrobot.eventbus.EventBus;


/**
 * 血糖
 */
public class ModifySex_Fragment extends BaseFragment implements View.OnClickListener {


    public static final String TAG = "ModifySex_Fragment";
    private TextView m_title_tv;
    private TextView m_righttitle_tv;
    private ImageView top_left_iv;


    private CombinView6 sex_cb6_man;
    private CombinView6 sex_cb6_women;

    int sex;


    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_modify_sex;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
        m_title_tv.setText(R.string.xingbie);
        m_righttitle_tv.setText(R.string.done);

        sex = UserSpUtil.readInt(Constants.IUser.SEX,0);//0男1女;
        setSex(sex);
        //FragmentUtil.replaceFragment(getChildFragmentManager(),R.id.ceshi_fl,new ModifyShHSet_Fragment());
    }
    @Override
    protected void setListener() {
        super.setListener();

        m_righttitle_tv.setOnClickListener(this);
        top_left_iv.setOnClickListener(this);
        sex_cb6_man.setOnClickListener(this);
        sex_cb6_women.setOnClickListener(this);

    }




    private void FV() {
        m_title_tv=mRootView.findViewById(R.id.m_title_tv);
        m_righttitle_tv=mRootView.findViewById(R.id.m_righttitle_tv);
        top_left_iv=mRootView.findViewById(R.id.top_left_iv);
        sex_cb6_man=mRootView.findViewById(R.id.sex_cb6_man);
        sex_cb6_women=mRootView.findViewById(R.id.sex_cb6_women);


    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.m_righttitle_tv:
                complete ();
                break;
            case R.id.top_left_iv:
                back();
                break;
            case R.id.sex_cb6_man:
                sex=0;
                setSex(sex);
                break;
            case R.id.sex_cb6_women:
                sex=1;
                setSex(sex);
                break;
        }
    }

    private void back() {
        if (mActivity!=null)mActivity.finish();
    }

    private void complete() {


        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        String authKey = MainActivity.getAuthKey();
        EhcNetUtils.getInstance().updatePersonSex(this, authKey, sex, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                LogUtils.i(TAG,s);
                if (isRequestSuccess&&baseResponse.resultCode==1){
                    //说明修改成功
                    ToastUtil.show(R.string.Modify_Success);
                    UserSpUtil.writeInt(Constants.IUser.SEX,sex);//0男1女
                    ScaleUser.getUser().setSex(sex);
                    // TODO: 2018/8/28   如果此时连接的是体脂秤 需要下发新的用户信息给秤
                    CommonEventBus commonEventBus = new CommonEventBus(TAG, MainActivity.TAG,sex,"设置了性别-需要重新下发性别");
                    EventBus.getDefault().post(commonEventBus);

                }else {
                    //说明修改失败
                    ToastUtil.show(R.string.Modify_Failed);
                }
            }
        });





    }
    public void setSex(int sex) {
        LogUtils.i(TAG,"------sex = "+sex);
        ViewUtilsEHC.setCb6Select(sex_cb6_man,false);
        ViewUtilsEHC.setCb6Select(sex_cb6_women,false);
        ViewUtilsEHC.setCb6Select(sex==0?sex_cb6_man:sex_cb6_women,true);
    }
}
